package org.example.compare.schedule;

import org.example.compare.dto.OrderDto;
import org.example.compare.dto.OrderProcess;
import org.example.compare.dto.SBoxData;
import org.example.compare.global.SchData;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class CSchDataCollector {
    public SBoxData findBoxByNum(String vBoxNum) {
        SBoxData pF = null;
        for (List<OrderDto> vOrderOneGroup : SchData.orderGroupByDeadLine) {
            //1.0、循环每个货期里面的订单
            for (OrderDto vOrder : vOrderOneGroup) {
                //2.0、循环每个部件
                for (List<SBoxData> pBoxs : vOrder.mCurBoxSetGroupByPart.values()) {
                    //3.0、循环每个筐
                    for (SBoxData pBox : pBoxs) {
                        if (pBox.getMBoxNum().equals(vBoxNum)) {
                            pF = pBox;
                            break;
                        }
                    }
                    if (pF != null) break;
                }
                if (pF != null) break;
            }
            if (pF != null) break;
        }
        return pF;
    }

    public OrderProcess findProcessByOrderPartSeq(SBoxData pBox) {
        OrderProcess pRet = null;
        //0.0循环每个货期
        for (List<OrderDto> vOrderOneGroup : SchData.orderGroupByDeadLine) {
            //1.0、循环每个货期里面的订单
            for (OrderDto vOrder : vOrderOneGroup) {
                if (vOrder.mLocalOrderNum.equals(pBox.getMLocalOrderNum())) {
                    if (vOrder.mOrderProcessSetGroupByPart.containsKey(pBox.getPartId())) {
                        //2.0、循环部件
                        Map<Integer, OrderProcess> processes = vOrder.mOrderProcessSetGroupByPart.get(pBox.getPartId());
                        for (OrderProcess process : processes.values()) {
                            if (process.getMiPartSeqNum() == pBox.getMiPartSeqNum()) {
                                pRet = process;
                                break;
                            }
                        }
                    }
                    //break;
                }
                if (null != pRet) {
                    break;
                }
            }
            if (null != pRet) {
                break;
            }
        }
        return pRet;
    }

    public OrderProcess findProcessByID(Long orderProcessId) {
        //遍历货期-订单-部件-工序
        for (List<OrderDto> vOrderOneGroup : SchData.orderGroupByDeadLine) {
            for (OrderDto vOrder : vOrderOneGroup) {
                for (Map<Integer, OrderProcess> pPartProcess : vOrder.mOrderProcessSetGroupByPart.values()) {
                    for (OrderProcess pProcess : pPartProcess.values()) {
                        //遍历货期-订单-部件-工序
                        if (orderProcessId == pProcess.getOrderProcessId()) {
                            return pProcess;
                        }
                    }
                }
            }
        }
        return null;
    }
}
